<template>
  <div class="divBox relative">
    <el-card class="box-card" style="min-height: 80vh">
      <div slot="header" class="clearfix" style="margin-bottom: -20px ;">
        <div class="container">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
            <el-form-item>
              <el-input v-model="queryForm.id" placeholder="请输入id" clearable @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="search()">查询</el-button>
              <el-button v-if="checkPermi(['impush:index:save'])" type="primary" @click="addOrUpdateHandle()">新增数据
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
                style="width: 100%;" size="mini" :highlight-current-row="true"
                :header-cell-style=" {fontWeight:'bold'}">

        <el-table-column prop="id" label="id" :show-overflow-tooltip="true" min-width="50"></el-table-column>
        <el-table-column prop="robotid" label="推送客服Id" :show-overflow-tooltip="true" min-width="100"></el-table-column>


        <el-table-column label="logo" width="80">
          <template slot-scope="scope">
            <el-image fit="scaleDown"
                      :src="scope.row.softwarepath" :preview-src-list="[scope.row.softwarepath]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="softwarename" label="应用名称" :show-overflow-tooltip="true" width="100"></el-table-column>
        <el-table-column prop="title" label="标题" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="content" label="内容" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="img" label="展示图">
          <template slot-scope="scope">
            <el-image style="width: 80px !important;height: 100% !important" fit="scaleDown"
                      :src="scope.row.img" :preview-src-list="[scope.row.img]"/>
          </template>
        </el-table-column>
        <el-table-column prop="sid" label="软件id/appid" :show-overflow-tooltip="true" width="160">
          <template slot-scope="scope"><span>{{scope.row.sid}}/{{scope.row.appid}}</span></template>
        </el-table-column>

        <el-table-column prop="userid" label="用户ID" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.userid">{{scope.row.userid}}</span>
            <span v-else>全部</span>
          </template>
        </el-table-column>
        <el-table-column prop="system" label="系统">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.system==0" type="success">全部</el-tag>
            <el-tag v-else-if="scope.row.system==1">Android</el-tag>
            <el-tag v-else type="warning">IOS</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="handlecount" label="执行次数" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="pushcount" label="推送人数" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="extra" label="附加内容" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.extra">{{scope.row.extra}}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status==0" type="info">未开始</el-tag>
            <el-tag v-else-if="scope.row.status==1">进行中</el-tag>
            <el-tag v-else type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createtime" label="创建时间" width="150px"></el-table-column>
        <el-table-column prop="finishtime" label="推送完成时间" width="150px"></el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text" v-if="checkPermi(['impush:index:update'])" size="small"
                       @click="addOrUpdateHandle(scope.row.id)">修改
            </el-button>
            <el-button type="text" v-if="checkPermi(['impush:index:delete'])" size="small"
                       @click="handlePush(scope.row)">推送
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="tableForm.pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="tableForm.pageSize"
        :total="tableForm.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-card>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './impush-add-and-update'
  import api from '@/api/api'
  import { checkPermi } from '@/utils/permission'

  export default {
    components: {
      AddOrUpdate
    },
    data() {
      return {
        dataList: [],
        queryForm: {
          id: ''
        },
        tableForm: {
          pageIndex: 1,
          pageSize: 10,
          total: 0
        },
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    mounted() {
      this.getDataList()
    },
    methods: {
      checkPermi,
      // 每页数
      sizeChangeHandle(val) {
        this.tableForm.pageSize = val
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.tableForm.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      search() {
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.dataListLoading = true
        let postData = {
          'order': 'desc',
          'column': 'id'
        }
        Object.assign(postData, this.queryForm, this.tableForm)
        //postData.name=api.getLikeValue( postData.name);
        api.post('rest/vabc/impush/list', postData).then(res => {
          this.dataList = res.datalist
          this.tableForm.total = res.total
        }).finally(() => {
          this.dataListLoading = false
        })
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      change(row) {
        let postData = { id: row.id }
        postData.status = row.status == 1 ? 0 : 1
        api.post('rest/vabc/impush/updateById', postData).then(res => {
          this.$message.success('操作成功')
          row.status = postData.status
          // this.getDataList()
        })
      },
      // 删除
      handlePush(info) {
        this.$confirm(`您确定对当前内容进行推送操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let postData = info
          api.post('rest/vabc/im/send1001Push', postData).then(res => {
            this.$message.success('操作成功')
            this.getDataList()
          })
        })
      }
    }
  }
</script>
